var entries = [];

function preProcessRSS(rssxml, id){
	if(rssxml == null){
		mwl.insertHTML('#verview', 'There was an error with feed url/data');
		return false;
	}

	var rssdata = XML2JS(rssxml);

	entries = [];
	for (var i = 0; i < rssdata.length; i++)
	{
		var entry = new RSSEntry(rssdata[i]);
		if (entry.xmlPubDate)
		{
			entries['xmlPubDate'] = entry.xmlPubDate;
			break;
		}
	}

	if(entries['xmlPubDate'])
		document.getElementById(id).innerHTML = entries['xmlPubDate'];
}

/**
 * Process and format the display of the received XML
 */
function processRSS(rssxml, vid, hid){
	if(rssxml == null){
		mwl.insertHTML('#verview', 'There was an error with feed url/data');
		return false;
	}

	if(vid == 'catPubDate')
	{
		preProcessRSS(rssxml, 'catPubDate');
		return;
	}

	var rssdata = XML2JS(rssxml);

	entries = [];
	for (var i = 0; i < rssdata.length; i++)
	{
		var entry = new RSSEntry(rssdata[i]);
		if (entry.title && entry.summary)
		{
			entries.push(entry);
			continue;
		}
		
		if(entry.xmlPubDate)
			entries['xmlPubDate'] = entry.xmlPubDate;
	}

	var outputstr = "";
	outputstr = getVerticalViewHTML();
	document.getElementById(vid).innerHTML = outputstr;
	
	
	outputstr = "";
	outputstr = getHorizViewHTML();
	document.getElementById(hid).innerHTML = outputstr;
	
	var scriptNode = document.createElement('SCRIPT');
	var text = "mwl.addSwipeLeftListener('#entry0', \"mwl.setGroupTarget('#btns','#btn1', 'show', 'hide');mwl.switchClass('#entries', 'entry0','entry1')\");"+
	"mwl.addSwipeLeftListener('#entry1', \"mwl.setGroupTarget('#btns','#btn2', 'show', 'hide');mwl.switchClass('#entries', 'entry1','entry2')\");"+
	"mwl.addSwipeLeftListener('#entry2', \"mwl.setGroupTarget('#btns','#btn3', 'show', 'hide');mwl.switchClass('#entries', 'entry2','entry3')\");"+
	"mwl.addSwipeLeftListener('#entry3', \"mwl.setGroupTarget('#btns','#btn4', 'show', 'hide');mwl.switchClass('#entries', 'entry3','entry4')\");"+
	"mwl.addSwipeLeftListener('#entry4', \"mwl.setGroupTarget('#btns','#btn5', 'show', 'hide');mwl.switchClass('#entries', 'entry4','entry5')\");"+
	"mwl.addSwipeLeftListener('#entry5', \"mwl.setGroupTarget('#btns','#btn6', 'show', 'hide');mwl.switchClass('#entries', 'entry5','entry6')\");"+
	"mwl.addSwipeLeftListener('#entry6', \"mwl.setGroupTarget('#btns','#btn7', 'show', 'hide');mwl.switchClass('#entries', 'entry6','entry7')\");"+
	"mwl.addSwipeLeftListener('#entry7', \"mwl.setGroupTarget('#btns','#btn8', 'show', 'hide');mwl.switchClass('#entries', 'entry7','entry8')\");"+
	"mwl.addSwipeLeftListener('#entry8', \"mwl.setGroupTarget('#btns','#btn9', 'show', 'hide');mwl.switchClass('#entries', 'entry8','entry9')\");"+

	"mwl.addSwipeRightListener('#entry9', \"mwl.setGroupTarget('#btns','#btn8', 'show', 'hide');mwl.switchClass('#entries', 'entry9','entry8')\");"+
	"mwl.addSwipeRightListener('#entry8', \"mwl.setGroupTarget('#btns','#btn7', 'show', 'hide');mwl.switchClass('#entries', 'entry8','entry7')\");"+
	"mwl.addSwipeRightListener('#entry7', \"mwl.setGroupTarget('#btns','#btn6', 'show', 'hide');mwl.switchClass('#entries', 'entry7','entry6')\");"+
	"mwl.addSwipeRightListener('#entry6', \"mwl.setGroupTarget('#btns','#btn5', 'show', 'hide');mwl.switchClass('#entries', 'entry6','entry5')\");"+
	"mwl.addSwipeRightListener('#entry5', \"mwl.setGroupTarget('#btns','#btn4', 'show', 'hide');mwl.switchClass('#entries', 'entry5','entry4')\");"+
	"mwl.addSwipeRightListener('#entry4', \"mwl.setGroupTarget('#btns','#btn3', 'show', 'hide');mwl.switchClass('#entries', 'entry4','entry3')\");"+
	"mwl.addSwipeRightListener('#entry3', \"mwl.setGroupTarget('#btns','#btn2', 'show', 'hide');mwl.switchClass('#entries', 'entry3','entry2')\");"+
	"mwl.addSwipeRightListener('#entry2', \"mwl.setGroupTarget('#btns','#btn1', 'show', 'hide');mwl.switchClass('#entries', 'entry2','entry1')\");"+
	"mwl.addSwipeRightListener('#entry1', \"mwl.setGroupTarget('#btns','#btn0', 'show', 'hide');mwl.switchClass('#entries', 'entry1','entry0')\");	";
	var scriptText = document.createTextNode(text);
	scriptNode.appendChild(scriptText);
	
	var bodyNode = document.getElementsByTagName('body')[0];
	bodyNode.appendChild(scriptNode);
	
}